//
//  MineWeatherReportViewController.m
//  baodongPro
//
//  Created by feng on 16/3/4.
//  Copyright © 2016年 proper. All rights reserved.
//
#import "MineWeatherAddCityViewController.h"
#import "MineWeatherReportViewController.h"
#import "MineWeatherCityListView.h"
#import "WeatherResultModel.h"
#import "WeatherCityModel.h"
#import "FBD_FMDBTool.h"
#import "MyRequest.h"
@implementation MineWeatherReportViewController
{
    WeatherResultModel*_weatherModel;
    MineWeatherCityListView* _rightCityListView;
    UITableView* _myTableView;
    WeatherCityModel* _selectCityModel;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    self.containedView.backgroundColor=[UIColor brownColor];
    [self setMyViewOfChidrenUI];
    

}

-(void)setMyViewOfChidrenUI
{
// 右边的ItemBar
    UIBarButtonItem* rightItemAdd=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addRightItemButtonPressed:)];
    
    // 右边的ItemBar
    UIBarButtonItem* rightItemSearch=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(rightItemButtonPressed:)];
    self.navigationItem.rightBarButtonItems=@[rightItemAdd,rightItemSearch];
    
    _myTableView=[self.view fbd_quickCreateUITabdWithleViewFrame:FBD_MKRect(0, 0, ScreenW, ScreenH-64) andDelegateAndDataSource:self];
    _myTableView.rowHeight=90;
    _myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.containedView addSubview:_myTableView];
    _myTableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self begainLoadNetWorkForWeatherWithCityModel:_selectCityModel];
    
        
    }];
    [_myTableView.mj_header beginRefreshing];


    
}
#pragma mark  ---------------------------------------------------NetWork-----------------------------------------begain
/**
 *      @author 冯宝东
 *
 *      加载网络查询 指定的城市天气状况
 */
-(void)begainLoadNetWorkForWeatherWithCityModel:(WeatherCityModel*)model
{
    [self showSVProgressWithTip:@"加载中"];
    NSMutableDictionary* paramDic=nil;
    [MyRequest setRequestManagerHeade:[NSMutableDictionary dictionaryWithObject:ReportApi_key forKey:@"apikey"]];
    if (model.m_area_id)
    {
        paramDic=[NSMutableDictionary dictionaryWithObject:model.m_area_id forKey:@"cityid"];
    }else
    {
        paramDic=[NSMutableDictionary dictionaryWithObject:@"101010100" forKey:@"cityid"];
    
    }

    
    [[MyRequest defultTool] getRequestWithURL:@"http://apis.baidu.com/apistore/weatherservice/cityid" withParameterDic:paramDic  withSuccessed:^(id result)
     {
         
         NSDictionary*resultDic=(NSDictionary*)result;
         NSString* resultMSG=[resultDic objectForKey:@"errMsg"];
         if ([resultMSG isEqualToString:@"success"])
         {
             NSDictionary* resultMSGDic=[resultDic objectForKey:@"retData"];
             _weatherModel=[[WeatherResultModel alloc] initWithDictionary:resultMSGDic];
             
         }

         [_myTableView.mj_header endRefreshing];
         [_myTableView reloadData];
         [self showSVProgressWithTip:resultMSG];
         [self disMissSVProgressWithDelaySecond:1.8];
         
         
         
         
     } withFailedBclock:^(id result) {
         
        [_myTableView.mj_header endRefreshing];
         [self showSVProgressWithTip:@"加载失败"];
         [self disMissSVProgressWithDelaySecond:2];
         
         
     } withPerBlock:^(float percentage) {
         NSLog(@"加载的进度。。。%.2f",percentage);
         
         
     } ];

}
-(void)begainLoadNetWorkForAllUseedCityList
{

    NSString* hostUrl= @"http://apis.baidu.com/apistore/weatherservice/citylist";
    [MyRequest setRequestManagerHeade:[NSMutableDictionary dictionaryWithObject:ReportApi_key forKey:@"apikey"]];
    NSMutableDictionary* paraDictionary=[NSMutableDictionary dictionaryWithObject:@"封丘" forKey:@"cityname"];
    [[MyRequest defultTool]  getRequestWithURL:hostUrl withParameterDic:paraDictionary withSuccessed:^(id result) {
        
        NSLog(@"封丘查询结果：%@",result);
        
        
        
    } withFailedBclock:^(id result) {
        
        
        
    } withPerBlock:^(float percentage) {
        
        
        
        
    }];










}






#pragma mark  ---------------------------------------------------NetWork-----------------------------------------end

#pragma mark  event
-(void)addRightItemButtonPressed:(id)sender
{

    MineWeatherAddCityViewController* addCityViewController=[[MineWeatherAddCityViewController alloc] initWithCityModelBlock:^(id indexOne) {
        _selectCityModel=indexOne;
//        [self begainLoadNetWorkForWeatherWithCityModel:indexOne];
            [_myTableView.mj_header beginRefreshing];
        NSString* tip=[NSString stringWithFormat:@"%@%@%@",_selectCityModel.m_province_cn,_selectCityModel.m_district_cn,_selectCityModel.m_name_cn];
        [FBD_FMDBTool db_yxInsertTableWithName:@"t_cityList" andName:tip withCityId:[_selectCityModel.m_area_id integerValue]];
        
    _rightCityListView.cityListArray=   [FBD_FMDBTool db_yxSearchTableViewName:@"t_cityList"];
        
    }];
    addCityViewController.title=@"添加城市";
    [self.navigationController pushViewController: addCityViewController animated:YES];



}


-(void)rightItemButtonPressed:(id)sender
{
    if (!_rightCityListView)
    {
        _rightCityListView=[[MineWeatherCityListView alloc] initWithFrame:CGRectMake(ScreenW, 0, ScreenW, screenH-64)withClickedIndexBlcok:^(NSInteger index) {
            [self moveRightCityListViewTapPressed:nil];
            _selectCityModel=[_rightCityListView.cityListArray objectAtIndex:index];
            [_myTableView.mj_header beginRefreshing];
            }];
        _rightCityListView.backgroundColor=[UIColor greenColor];
        _rightCityListView.alpha=.9;
        UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moveRightCityListViewTapPressed:)];
        [_rightCityListView.backGroundView addGestureRecognizer:tap];
        [self.containedView addSubview:_rightCityListView];
        
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        [_rightCityListView setView_orignX:0];
    }];
    _rightCityListView.cityListArray=   [FBD_FMDBTool db_yxSearchTableViewName:@"t_cityList"];


}
-(void)moveRightCityListViewTapPressed:(UITapGestureRecognizer*)tap
{

    [UIView animateWithDuration:0.3 animations:^{
        
        [_rightCityListView setView_orignX:ScreenW];
    
    }];


}


#pragma mark ---UITableViewDataSource&&UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _weatherModel.m_valueArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* indetifier=@"MyCells";
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier ];
        cell.selectionStyle=0;
        
    }

    cell.backgroundColor=[cell randomColor];
    
    NSInteger row=indexPath.row;
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@:%@",[_weatherModel.m_tipArray objectAtIndex:row],[_weatherModel.m_valueArray objectAtIndex:row]];

    
    return cell;
    
}









@end
